package com.zixun.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zixun.Models.Message;
import com.zixun.R;

import java.util.List;


public class GrowUpAdapter extends RecyclerView.Adapter<GrowUpAdapter.MessageViewHolder> {
    private List<Message> datas;
    private Context context;
    private LayoutInflater inflater;


    public OnItemClickListener mListener;


    public interface OnItemClickListener {
        void onItemClick(int position, Message data);
    }

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }


    public GrowUpAdapter(Context context, List<Message> datas) {
        this.context = context;
        this.datas = datas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View layout = inflater.inflate(R.layout.growup_item_layout, parent, false);
        return new MessageViewHolder(layout);

    }


    @Override
    public void onBindViewHolder(MessageViewHolder viewHolder, final int position) {
        final String data = datas.get(position).getTitle();
        if (viewHolder instanceof MessageViewHolder) {
            ((MessageViewHolder) viewHolder).txt_content.setText(data);
            if (viewHolder != null) {
                ((MessageViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(position, datas.get(position));
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class MessageViewHolder extends RecyclerView.ViewHolder {

        public TextView txt_content;

        public MessageViewHolder(View itemView) {
            super(itemView);
            if (itemView == null) return;
            txt_content = (TextView) itemView.findViewById(R.id.txt_content);
        }
    }

}
